var eap_sa_privilege = {
	init: function(modelPrivilege) {
		var $activeDom = getActiveDom();
		$('[cmbname="category"],[cmbname="model"],[cmbname="method"]', $activeDom).on('select', function() {
			var hiddenObj = $(this).prev().get(0); 
			eap_sa_privilege.loadFieldPrivilege(hiddenObj, modelPrivilege);
		});
		
		$('#fieldPermissionCtn', $activeDom).find('input[type="radio"]').click(function() {
			if (this.checked) {
				eap_sa_privilege._initCheckboxEvent(this);
			}
		});
	},
	
	loadFieldPrivilege: function(selectObj, modelPrivilege) {
		var params = {};
		
		var curTabObj = getActiveDom();
		var $category = $('[name=category]', curTabObj);
		params['category'] = $category.get(0).value;
		params['modelPrivilege'] = modelPrivilege;
		
		if ($category.get(0) != selectObj) {	// category --> model --> method
			var $model = $('[name=model]', curTabObj);
			params['model'] = $model.get(0).value;
			if ($model.get(0) != selectObj) {
				var $method = $('[name=method]', curTabObj);
				params['method'] = $method.get(0).value;
			}
		}
		params['userId'] = $('[name=userId]', curTabObj).val();
	
		$.ajax({
			url: g_base_site + '/espirit/eap/sa/privilege.showFieldPrivilege.html',
			data:  params,
			beforeSend: function() {
				$('#fieldPermissionCtn').html('');
				$('#fieldPermissionCtn').addClass("loading");
			},
			success: function(data) {
				var $activeDom = getActiveDom();
				var $fieldPermissionCtn = $('#fieldPermissionCtn', $activeDom);
				
				$fieldPermissionCtn.removeClass('loading');
				$fieldPermissionCtn.html(data);
				$fieldPermissionCtn.find('input[type="radio"]').off('click').click(function() {
					if (this.checked) {
						eap_sa_privilege._initCheckboxEvent(this);
					}
				});
			}
		});
	},
	
	_initCheckboxEvent: function(self) {
		// hide, read, update 
		// click hide, the other flag reset to 0
		// click read, the hide flag reset to 0
		// click update, the hide flag reset to 0
		var $td = $(self).closest('td');
		var selfIndex = $td.index();
		var $tr = $td.parent();
		var hideImagObj = $tr.children().eq(1).find('.clickStyle');
		var readImagObj = $tr.children().eq(2).find('.clickStyle');
		var updateImagObj = $tr.children().eq(3).find('.clickStyle');
		
		if (selfIndex == 1) {	// first td is the description field, the second td is hidden field
			eap_sa_privilege.switchFieldFlag(readImagObj, 0);
			eap_sa_privilege.switchFieldFlag(updateImagObj, 0);
		} else {
			eap_sa_privilege.switchFieldFlag(hideImagObj, 0);
		}
		self.checked = true;
	},
	
	switchFieldFlag: function(imgObj, flag) {
		var $imgFlag = $(imgObj);
		if ($imgFlag.length == 0) {
			return;
		}
		
		var nextFlag;
		if (flag != null) {
			nextFlag = flag;
		} else {
			var curImgFlag = Number($imgFlag.attr('imgFlag'));
			nextFlag = ++curImgFlag;
		}
		if (nextFlag > 4) {
			nextFlag = 0;
		}
		$imgFlag.attr('src', g_base + '/static/espirit/images/permission/flag' + nextFlag + '.gif');
		$imgFlag.attr('imgFlag', nextFlag);
		
		var $privilege = $imgFlag.parent().find('input[type="hidden"]');
		$privilege.val(nextFlag);
		var $fieldPermissionRadio = $imgFlag.parent().find('input[type="radio"]');
		var $curTd = $imgFlag.closest('td');
		if (nextFlag == 0) {			// uncheck current td
			$fieldPermissionRadio.get(0).checked = false;
			
			var $targetTd = $curTd.prev();
			var strImgFlag = $targetTd.find('.clickStyle').attr('imgFlag');
			if (strImgFlag || strImgFlag == '0') {		// curTd is READWRITE
				var targetTdFlag = Number(strImgFlag);
				if (targetTdFlag == 0) {
					$targetTd = $targetTd.prev();
				}
			} else {					// curTd is READ
				var nextFlag = Number($curTd.next().find('.clickStyle').attr('imgFlag'));
				if (nextFlag) {
					$targetTd = $curTd.next();
				}
			}
			$targetTd.find('input[type="radio"]').get(0).checked = true;
		} else {
			var nextTdFlag = $curTd.next().find('.clickStyle').attr('imgFlag');
			if (!nextTdFlag || nextTdFlag == '0') {
				$fieldPermissionRadio.get(0).checked = true;
			}
		}
	},
	
	saveFieldPrivilege: function(btn) {
		var $btn = $(btn);
		if ($btn.hasClass("disabled")) {
			return;
		}
		$btn.addClass("disabled");
		var $form = $('#userFiedPermissionForm');
		addLoading($form.children('table'));
		var ret = ajaxSubmit($form, {
			data: {_resultCode:'$json'},
			success:function(data) {
				removeLoading($form.children('table'));
				
				var $data = $(data);
				if ($data.hasClass('error')) {
					showError($data.html());
				} else {
					showSuccess(getText('commons.success'), true);
				}
			}
		});
		if (!ret) {
			$btn.removeClass("disabled");
		}
	},
	
	resetFieldPrivilege: function() {
		openMainTab(g_base_site + '/espirit/eap/sa/privilege.showFieldPrivilege.html','model.espirit.eap.sa.user.fieldPermission');
	}
}